<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="depts" default-expand-all :expand-on-click-node="false" :props="defaultProps">
        <template #default="{ data }">
          <!-- el-row 行 el-col 列 -->
          <el-row style="width: 100%; height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager-name">
                {{ data.managerName }}
              </span>
              <!--
                operteDepts($event)  ===> operteDepts
               -->
              <el-dropdown @command="operteDepts($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 封装添加部门的组件 -->
    <!-- <AddDept
      :show-dialog="showDialog"
      @update:showDialog="showDialog = $event"
    /> -->
    <AddDept
      ref="addDeptCom"
      :depart-id="departId"
      :show-dialog.sync="showDialog"
      @updateDepartment="getDepts"
    />
  </div>
</template>
<script>
import AddDept from './components/AddDept.vue'
import { getDepartment, delDepartment } from '@/api/department'
import { translateListToTreeData } from '@/utils/index'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      showDialog: false,
      departId: null, // 点击部门的id
      depts: [
        {
          name: '传智教育',
          managerName: '管理员',
          children: [
            {
              name: '总裁办',
              managerName: '张三'
            },
            {
              name: '行政部',
              managerName: '王五'
            },
            {
              name: '人事部',
              managerName: '赵六',
              children: [
                {
                  name: '人事行政部',
                  managerName: '田七'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        label: 'name', // 标签的属性
        children: 'children' // 子元素的属性
      }
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    async operteDepts(type, id) {
      if (type === 'add') {
        this.departId = id
        // 显示弹层 - 控制弹层显示隐藏变量 / true
        this.showDialog = true
      } else if (type === 'edit') {
        this.departId = id
        // 显示弹层 - 控制弹层显示隐藏变量 / true
        this.showDialog = true
        // 调用子组件的获取部门详情的方法
        // 1. 获取子组件的实例 ref 和 $refs
        // 2. 调用实例身上的方法
        // vue更新dom的过程是异步 - 提高性能
        // 一旦数据发生了变化, 组件实例和dom更新渲染是异步的
        // $nextTick 获取到更新后的dom和组件实例
        this.$nextTick(() => {
          this.$refs.addDeptCom.getDepartmentDetail()
        })
      } else {
        // 删除
        // 1. 弹出确认框
        await this.$confirm('确认要删除此部门么?', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        // 2. 调用删除的API
        await delDepartment(id)
        // 3. 提示用户
        this.$message.success('删除部门成功')
        // 4. 重新获取部门列表
        this.getDepts()
      }
    },
    async getDepts() {
      const res = await getDepartment()
      // this.depts = res
      this.depts = translateListToTreeData(res, 0)
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.manager-name {
  width: 50px;
  display: inline-block;
  margin-right: 10px;
}
</style>
